package com.youease.chris.view.mediator
{
	import com.youease.chris.control.CreateProjectCommand;
	import com.youease.chris.view.CreateProjectTitleWindow;
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.utils.Common;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class CreateProjectMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "CreateProjectMediator";
		public static var INDEX:int = 0;
		
		public function CreateProjectMediator(viewComponent:Object)
		{
			super(NAME+INDEX, viewComponent);
			createProject.create.addEventListener(MouseEvent.CLICK,onCreateProject);
			createProject.select.addEventListener(MouseEvent.CLICK,onView);
			INDEX++;
		}
		
		public function onView(evt:MouseEvent):void {
			var file:File = new File();
			file.browseForDirectory("选择目录");
			file.addEventListener(Event.SELECT,onSelect);
		}
		
		public function onSelect(evt:Event):void {
			var file:File = evt.target as File;
			createProject.projectDir.text = GlobalObject.projectDir = file.nativePath;
		}
		
		public function onCreateProject(evt:MouseEvent):void {
			var projectName:String = createProject.projectName.text;
			if(Common.trim(projectName)=="") {
				trace("工程名不能为空");
				createProject.nameError.visible = true;
				return;
			}else {
				createProject.nameError.visible = false;
			}
			if(Common.trim(createProject.projectDir.text)=="") {
				trace("工程地址不能为空");
				createProject.dirError.visible = true;
				return;
			}else {
				createProject.dirError.visible = false;
			}
			sendNotification(CreateProjectCommand.CREATE_PROJECT,projectName);
		}
		
		override public function listNotificationInterests():Array {
			return [
				CreateProjectCommand.CREATE_PROJECT_COMPLETE
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case CreateProjectCommand.CREATE_PROJECT_COMPLETE:
					createProject.closeWindow();
					break;
			}
		}
		
		public function get createProject():CreateProjectTitleWindow {
			return this.viewComponent as CreateProjectTitleWindow;
		}
	}
}